//
//  FirstViewController.m
//  WisdomZhuHai
//
//  Created by yewei on 13-8-21.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "FirstViewController.h"
#import "Common.h"
#import "AppDelegate.h"
#import "SpecialMessageViewController.h"
#import "TabBarController.h"
#import "TransforController.h"
#import "ZFModalTransitionAnimator.h"
#import "newSideBarViewController.h"
#import "GoodDetailViewController.h"
#import "GlobalCellViewController.h"
#import "EmptyViewController.h"
#import "ProtocolDefine.h"

@interface FirstViewController ()<UITableViewDelegate, scrollViewDelegate, globalSearchDelegate>

//@property (nonatomic,retain) SpecialMessageViewController *specialMessageCtrl;
@property (nonatomic,retain) SpecialMessageViewController *homePageViewCtrl;
@property (nonatomic, strong) UIView *view_head;
@property (nonatomic, strong) ZFModalTransitionAnimator *animator;
@property BOOL dragable;
@end

@implementation FirstViewController
@synthesize homePageViewCtrl;

- (id)init
{
    self = [super init];
    if(self)
    {
        self.tabBarItem.image = [UIImage imageNamed:@"Information/首页2.png"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"Information/首页1.png"];
        self.tabBarItem.title = @"首页";//NSLocalizedString(@"HomePage", nil);
    }
    return self;
}

-(void)dealloc
{
    [homePageViewCtrl release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.homePageCtrl = [[[HomePageViewController alloc] initWithStyle:UITableViewStylePlain]autorelease];
//    homePageCtrl.navController = self.navigationController;
//    homePageCtrl.frame = self.view.bounds;
//    [self.view addSubview:homePageCtrl.view];
//    self.homePageViewCtrl = [[[SpecialMessageViewController alloc] initWithStyle:UITableViewStylePlain]autorelease];
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStyleBordered target:self action:@selector(search)];
    UIBarButtonItem *phoneItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_main_phone"] style:UIBarButtonItemStyleBordered target:self action:@selector(call)];
    NSArray *itemArray = [[NSArray alloc] initWithObjects:phoneItem, searchItem, nil];
    //    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStyleBordered target:self action:@selector(search)];
    self.navigationItem.rightBarButtonItems = itemArray;
    self.homePageViewCtrl = [TransforController sharedInstanceOfSpecialMessageViewController];
    //homePageViewCtrl.navController = self.navigationController;
    homePageViewCtrl.frame = self.view.bounds;
    homePageViewCtrl.navController = self.navigationController;
    homePageViewCtrl.delegate = self;
    [self.view addSubview:homePageViewCtrl.view];
    [self viewHeadSetup];
    self.dragable = NO;
    //
    Class cls = NSClassFromString(@"UMANUtil");
    SEL deviceIDSelector = @selector(openUDIDString);
    NSString *deviceID = nil;
    if(cls && [cls respondsToSelector:deviceIDSelector]){
        deviceID = [cls performSelector:deviceIDSelector];
    }
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@{@"oid" : deviceID}
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil];
    
    NSLog(@"%@ 这是友盟测试设备ID的识别代码", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
    
}

- (void)changeHeadViewAlpha:(CGFloat)offset{
    if(offset > 5 || offset < 150){
        [self.view_head setAlpha:offset / 100];
    }
}

//打开用户定制按钮
-(void)openCustomizeBtn:(UIGestureRecognizer*)onTap
{
    NSLog(@"=======>FirstViewController======>openCustomizeBtn %@",onTap);
        newSideBarViewController *newSideBar = [[[newSideBarViewController alloc] init] autorelease];
        newSideBar.modalPresentationStyle = UIModalPresentationCustom;
        
        self.animator = [[ZFModalTransitionAnimator alloc] initWithModalViewController:newSideBar];
        self.animator.dragable = self.dragable; //是否滑动返回主页
        self.animator.bounces = NO;
        self.animator.behindViewAlpha = 0.5f;
        self.animator.behindViewScale = 0.5f;
        
        //    NSString *title = [sender titleForState:UIControlStateNormal];
        //    if ([title isEqualToString:@"Left"]) {
//        self.animator.direction = ZFModalTransitonDirectionLeft;
        //    } else if ([title isEqualToString:@"Right"]) {
                self.animator.direction = ZFModalTransitonDirectionRight;
        //    } else {
        //        self.animator.direction = ZFModalTransitonDirectionBottom;
        //    }
        
        newSideBar.transitioningDelegate = self.animator;
    newSideBar.hidesBottomBarWhenPushed = YES;
        [self presentViewController:newSideBar animated:YES completion:nil];
}

//创建顶部条目
-(void)viewHeadSetup
{
    self.view_head = [[UIView alloc]initWithFrame:CGRectMake(0, -45, 320, 45)];
    self.view_head.backgroundColor = [Common colorWithRGB:0x18aaf6];
    UILabel *lb_headTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view_head.bounds.size.width, self.view_head.bounds.size.height)];
    lb_headTitle.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18.0];
    lb_headTitle.textColor = [UIColor whiteColor];
    lb_headTitle.textAlignment = UITextAlignmentCenter;
    lb_headTitle.text = @"首页";
    [self.view_head addSubview:lb_headTitle];
    [self.view_head setAlpha:0];
    UIView *customizeBtn = [[UIView alloc] initWithFrame:CGRectMake(260, [UIScreen mainScreen].bounds.size.height - 200, 50, 50)];
    customizeBtn.backgroundColor = [UIColor clearColor];
    UIImageView *customizeBtnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, customizeBtn.bounds.size.width, customizeBtn.bounds.size.height)];
    customizeBtnImageView.image = [UIImage imageNamed:@"siftSideBar/customizeBtn"];
    customizeBtnImageView.alpha = 0.7f;
    customizeBtnImageView.layer.shadowOffset = CGSizeMake(4, 4);
    customizeBtnImageView.layer.shadowOpacity = 0.5;
    customizeBtnImageView.layer.shadowRadius = 2.0;
    UITapGestureRecognizer *onTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openCustomizeBtn:)];
    [customizeBtn addSubview:customizeBtnImageView];
    [customizeBtn addGestureRecognizer:onTap];
    [self.view addSubview:customizeBtn];
    [self.view addSubview:self.view_head];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [homePageCtrl viewWillAppear:animated];
    [self.homePageViewCtrl viewWillAppear:animated];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [homePageCtrl viewWillDisappear:animated];
    [self.homePageViewCtrl viewWillDisappear:animated];
    
}

-(void)globalSearchCell:(NSString*)keyword
{
    NSLog(@"globalSearchCell");
    GlobalCellViewController *globalCellCtrl = [[GlobalCellViewController alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 65) style:UITableViewStylePlain];
    globalCellCtrl.keyword = keyword;
    globalCellCtrl.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:globalCellCtrl animated:YES];
}

- (void)search
{
    NSLog(@"search");
    EmptyViewController *searchCtrl = [[EmptyViewController alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    searchCtrl.searchDelegate = self;
    searchCtrl.isSearch = YES;
    [self.navigationController presentViewController:searchCtrl animated:YES completion:nil];
}

- (void)call
{
    NSLog(@"call");
    [[Common shareInstance] callWithPhoneString:OKSERVICEPHONE inView:self.view];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
